<?php

namespace App\Listeners\TopicCreated;

use App\Events\TopicCreated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class UpdateUserTopicCount
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  TopicCreated  $event
     * @return void
     */
    public function handle(TopicCreated $event)
    {
        $topic = $event->topic;
        $user = $topic->user;
        $counts = $user->topics()->withTrashed()->count();
        $user->topic_count = $counts;
        $user->save();

        logger(sprintf(
            "%s, user: %s, topic count: %s",
            __METHOD__, $user->id, $counts
        ));

    }
}
